/* Copyright 2008. All rights reserved. */
package org.bling.parser.tags;

import org.bling.parser.BlingParserException;
import org.bling.parser.style.StyleParser;
import org.bling.util.Util;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;

/**
 * renders an SWT Text field
 * 
 * @author DISBROB
 * 
 */
public class TextTag extends Tag {

	protected Text text;
	protected String limit = "";
	protected String property = "";
	protected String style = "";
	protected String echo = "";

	public void setEcho(String echo) {
		this.echo = echo;
	}
	
	public void setStyle(String style) {
		this.style = style;
	}
	
	public void setProperty(String property) {
		this.property = property;
	}
	
	public void setLimit(String limit) {
		this.limit = limit;
	}

	@Override
	public int doStartTag() throws BlingParserException {
		super.doStartTag();

		if (!(parent.getWidget() instanceof Composite)) {
			throw new BlingParserException(
					"Label tags can only be inside of composites");
		}

		Composite composite = (Composite) parent.getWidget();
		text = new Text(composite, SWT.BORDER);
		text.setData(property);
		

		if (context.getAttribute(property) != null) {
		text.setText(context.getAttribute(property).toString());
		}
		
		// FIXME: Put id on text widget
		// text.setData(id);
		if (!Util.isBlankOrNull(limit)) {
			text.setTextLimit(Integer.parseInt(limit));
		}

		StyleParser.applyStyle((Control) getWidget(), style);
		
		if (!Util.isBlankOrNull(echo)) {
			Character echoChar = echo.charAt(0);
			text.setEchoChar(echoChar);
		}
		
		return EVAL_PAGE;
	}

	@Override
	public Widget getWidget() throws BlingParserException {
		return text;
	}

}
